package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.FunctionUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.BExpVo;
import com.sxbbc.common.core.entity.jz.Address;
import com.sxbbc.common.core.entity.jz.BExpConfig;
import com.sxbbc.common.core.entity.jz.BExpDetail;
import com.sxbbc.common.core.mapper.jz.AddressMapper;
import com.sxbbc.common.core.mapper.jz.BExpConfigMapper;
import com.sxbbc.common.core.service.IBExpConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.IBExpDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 商家运费配置表，金额，数量/重量免邮可同时存在 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-31
 */
@Service
public class BExpConfigServiceImpl extends ServiceImpl<BExpConfigMapper, BExpConfig> implements IBExpConfigService {

    @Autowired
    private IBExpDetailService bExpDetailService;

    @Autowired
    private BExpConfigMapper bExpConfigMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public RestResponse addExpModel(BExpVo bExpVo) {

        QueryWrapper<BExpConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("bid", bExpVo.getBid()).eq("is_farm", StaticUtils.STATUS_YES);
        int count = super.count(wrapper);
        if (count >= 1) {
            throw new ThrowJsonException("已存在物流提鸡运费模板,不可重复添加");
        }
        boolean save = false;
        BExpConfig bExpConfig = new BExpConfig();
        bExpConfig.setBid(bExpVo.getBid());
        bExpConfig.setName(bExpVo.getName());
        bExpConfig.setMoney(bExpVo.getMoney());
        bExpConfig.setVals(bExpVo.getVals());
        bExpConfig.setStatus(StaticUtils.STATUS_YES);
        bExpConfig.setValuationType(bExpVo.getValuationType());
        bExpConfig.setIsFarm(bExpConfig.getIsFarm());
        save = super.save(bExpConfig);
        if (!save) {
            throw new ThrowJsonException("保存运费模板配置信息失败");
        }
        for (int i = 0; i < bExpVo.getBeginNum().length; i++) {
            BExpDetail bExpDetail = new BExpDetail();
            bExpDetail.setExpid(bExpConfig.getId());
            bExpDetail.setBeginNum(bExpVo.getBeginNum()[i]);
            //bExpDetail.setBeginNum(new BigDecimal(bExpVo.getBeginNum()[i]));
            bExpDetail.setBeginMoney(bExpVo.getBeginMoney()[i]);
            bExpDetail.setAddNum(bExpVo.getAddNum()[i]);
            bExpDetail.setAddMoney(bExpVo.getAddMoney()[i]);
            if (i > 0) {
                bExpDetail.setProid(bExpVo.getProId()[i]);
                bExpDetail.setCityid(bExpVo.getCityId()[i]);
            }
            if (i == 0) {
                bExpDetail.setDefaultFlag(StaticUtils.STATUS_YES);
            } else {
                bExpDetail.setDefaultFlag(StaticUtils.STATUS_NO);
            }
            save = bExpDetailService.save(bExpDetail);
        }
        if (!save) {
            throw new ThrowJsonException("保存运费模板配置信息失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public RestResponse editExpModel(BExpVo bExpVo) {
        boolean save = false;
        BExpConfig expConfig = super.getById(bExpVo.getId());
        if (expConfig == null) {
            return GetRest.getFail("该模板信息不存在");
        }

        if (CalcuUtils.getInstance().isEquals(bExpVo.getIsFarm(), StaticUtils.STATUS_YES)) {
            QueryWrapper<BExpConfig> bexwrapper = new QueryWrapper<>();
            bexwrapper.eq("bid", bExpVo.getBid()).eq("is_farm", StaticUtils.STATUS_YES).ne("id", bExpVo.getId());
            int count = super.count(bexwrapper);
            if (count >= 1) {
                throw new ThrowJsonException("已存在物流提鸡运费模板,不可重复添加");
            }
        }
        expConfig.setName(bExpVo.getName());
        expConfig.setMoney(bExpVo.getMoney());
        expConfig.setVals(bExpVo.getVals());
        expConfig.setValuationType(bExpVo.getValuationType());
        expConfig.setIsFarm(bExpVo.getIsFarm());
        save = super.updateById(expConfig);
        if (!save) {
            throw new ThrowJsonException("更新运费模板信息失败");
        }
        QueryWrapper<BExpDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("expid", expConfig.getId());
        save = bExpDetailService.remove(wrapper);
        if (!save) {
            throw new ThrowJsonException("更新运费模板详情失败");
        }
        for (int i = 0; i < bExpVo.getBeginNum().length; i++) {
            BExpDetail bExpDetail = new BExpDetail();
            bExpDetail.setExpid(expConfig.getId());
            bExpDetail.setBeginNum(bExpVo.getBeginNum()[i]);
            bExpDetail.setBeginMoney(bExpVo.getBeginMoney()[i]);
            bExpDetail.setAddNum(bExpVo.getAddNum()[i]);
            bExpDetail.setAddMoney(bExpVo.getAddMoney()[i]);
            if (i > 0) {
                bExpDetail.setProid(bExpVo.getProId()[i]);
                bExpDetail.setCityid(bExpVo.getCityId()[i]);
            }
            if (i == 0) {
                bExpDetail.setDefaultFlag(StaticUtils.STATUS_YES);
            } else {
                bExpDetail.setDefaultFlag(StaticUtils.STATUS_NO);
            }
            save = bExpDetailService.save(bExpDetail);
            if (!save) {
                throw new ThrowJsonException("保存运费模板配置信息失败");
            }
        }
        return GetRest.getSuccess("保存成功");
    }

    @Override
    public List<BExpConfig> queryBusinessExpList(Integer bid) {
        QueryWrapper<BExpConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("bid", bid);
        List<BExpConfig> expList = super.list(wrapper);
        if (!CollectionUtils.isEmpty(expList) && expList.size() > 0) {
            for (BExpConfig e : expList) {
                QueryWrapper<BExpDetail> detailWrapper = new QueryWrapper<>();
                detailWrapper.eq("expid", e.getId());
                List<BExpDetail> details = bExpDetailService.list(detailWrapper);
                if (details.size() > 0) {
                    for (BExpDetail detail : details) {
                        StringBuffer buffer = new StringBuffer();
                        if (StringUtils.isNotBlank(detail.getProid())) {
                            //省份
                            List<Integer> proIds = FunctionUtils.getIntegerList(detail.getProid().split(","));
                            int i = 0;
                            for (Integer item : proIds) {
                                Address address = addressMapper.selectById(item);
                                buffer.append(address.getName());
                                String[] cityStrids = detail.getCityid().split("\\^\\^");
                                String[] proCity = cityStrids[i].split(",");
                                buffer.append("【");
                                for (String s : proCity) {
                                    Address city = addressMapper.selectById(s);
                                    buffer.append(city.getName() + ",");
                                }
                                buffer.append("】\t\n");
                                i++;
                            }
                            detail.setAddressStr(buffer.toString());
                            detail.setAddressStr(detail.getAddressStr().replace(",】", "】"));
                        }
                    }
                }
                e.setExpDetailList(details);
            }
        }
        return expList;

    }

    @Override
    public BExpConfig selectChickenExpConfig() {
        return bExpConfigMapper.selectChickenExpConfig();
    }
}
